package com.opengroupe.service.tablet.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.transaction.annotation.Transactional;

import com.opengroupe.dao.CandidatDao;
import com.opengroupe.dao.CandidatReponseQuestionDao;
import com.opengroupe.dao.CategorieDao;
import com.opengroupe.entities.Candidat;
import com.opengroupe.entities.CandidatReponseQuestion;
import com.opengroupe.entities.CandidatReponseQuestionId;
import com.opengroupe.entities.Categorie;
import com.opengroupe.entities.Question;
import com.opengroupe.entities.Reponse;
import com.opengroupe.service.tablet.TabletService;

public class TabletServiceImpl implements TabletService {

	@Autowired
	private CandidatReponseQuestionDao candidatReponseQuestionDao;

	@Autowired
	private CandidatDao candidatDao;
	
	@Autowired
	private CategorieDao categorieDao;

	private static final Log logger = LogFactory.getLog(TabletServiceImpl.class);

	public List<CandidatReponseQuestionId> repondreQuestion(Candidat candidat, Question question,List<Reponse> reponses) throws Exception {
		
		List<CandidatReponseQuestionId> returnedCRQId = new ArrayList<CandidatReponseQuestionId>(reponses.size());

		for (Reponse reponse : reponses) {
			
			CandidatReponseQuestion candidatReponseQuestion = new CandidatReponseQuestion();

			candidatReponseQuestion.setCandidat(candidat);
			candidatReponseQuestion.setQuestion(question);
			candidatReponseQuestion.setReponse(reponse);
			candidatReponseQuestion.setId(new CandidatReponseQuestionId(
					candidat.getIdCandidat(), question.getIdQuestion(),
					reponse.getIdReponse()));

			try {
				candidatReponseQuestion = candidatReponseQuestionDao.persist(candidatReponseQuestion);
				returnedCRQId.add(candidatReponseQuestion.getId());
			} catch (DataAccessException e) {
				logger.error("Erreur lors de la sauvegarde de la réponse du candidat");
				throw e;
			} catch (Exception e) {
				logger.error("Erreur lors de la sauvegarde de la réponse du candidat");
				throw e;
			}
		}
		return returnedCRQId;
	}

	@Transactional
	public List<CandidatReponseQuestion> affecterQuestionnaire(Candidat candidat, Categorie categorie) throws DataAccessException {
		logger.debug("affectation du questionnaire "+categorie.getLibelleCategorie()+" au candidat"+candidat.getNomCandidat());
		
		candidat = candidatDao.findByEmail(candidat.getEmailCandidat());
		categorie = categorieDao.findByLibelle(categorie.getLibelleCategorie());
		List<CandidatReponseQuestion> questionnaire = new ArrayList<CandidatReponseQuestion>(); 
		List<Question> questions;
		
		try{
			questions = categorie.getQuestions();
		} catch (DataAccessException e) {
			throw e;
		}
		
		for(Question question : questions){
			CandidatReponseQuestion candidatReponseQuestion = new CandidatReponseQuestion();
			candidatReponseQuestion.setCandidat(candidat);
			candidatReponseQuestion.setQuestion(question);
			
			questionnaire.add(candidatReponseQuestion);
		}
		
		return questionnaire;
	}

	@Override
	public List<CandidatReponseQuestion> deleteCRQs(Candidat candidat,
			Question question) {
		return candidatReponseQuestionDao.deleteCRQs(candidat, question);
	}
	
	@Override
	public Candidat creerCandidat(Candidat candidat) throws Exception {
		try{
			return candidatDao.persist(candidat);
		} catch (DataAccessException e) {
			throw e;
		}
	}
}
